#coding=utf-8
#Boa:Dialog:DlgAccount

import wx
from product import CDataAccount
from ctrlAider import *
import DlgAccountEditor

def create(parent):
    return DlgAccount(parent)

[wxID_DLGACCOUNT, wxID_DLGACCOUNTBTNADD, wxID_DLGACCOUNTBTNDEL, 
 wxID_DLGACCOUNTBTNMODIFY, wxID_DLGACCOUNTLCACCOUNT, 
] = [wx.NewId() for _init_ctrls in range(5)]

class DlgAccount(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DLGACCOUNT, name=u'DlgAccount',
              parent=prnt, pos=wx.Point(273, 265), size=wx.Size(605, 361),
              style=wx.DEFAULT_DIALOG_STYLE, title=u'\u8d44\u91d1\u8d26\u6237')
        self.SetClientSize(wx.Size(597, 327))

        self.btnAdd = wx.Button(id=wxID_DLGACCOUNTBTNADD, label=u'\u65b0\u5efa',
              name=u'btnAdd', parent=self, pos=wx.Point(40, 32),
              size=wx.Size(75, 24), style=0)
        self.btnAdd.Bind(wx.EVT_BUTTON, self.OnBtnAddButton,
              id=wxID_DLGACCOUNTBTNADD)

        self.btnModify = wx.Button(id=wxID_DLGACCOUNTBTNMODIFY,
              label=u'\u4fee\u6539', name=u'btnModify', parent=self,
              pos=wx.Point(144, 32), size=wx.Size(75, 24), style=0)
        self.btnModify.Bind(wx.EVT_BUTTON, self.OnBtnModifyButton,
              id=wxID_DLGACCOUNTBTNMODIFY)

        self.btnDel = wx.Button(id=wxID_DLGACCOUNTBTNDEL, label=u'\u5220\u9664',
              name=u'btnDel', parent=self, pos=wx.Point(256, 32),
              size=wx.Size(75, 24), style=0)
        self.btnDel.Bind(wx.EVT_BUTTON, self.OnBtnDelButton,
              id=wxID_DLGACCOUNTBTNDEL)

        self.lcAccount = wx.ListCtrl(id=wxID_DLGACCOUNTLCACCOUNT,
              name=u'lcAccount', parent=self, pos=wx.Point(40, 80),
              size=wx.Size(512, 208), style=wx.LC_REPORT)

    def __init__(self, parent):
        self._init_ctrls(parent)
        
        self.myInit()
        
    def myInit(self):
        cols = [(CDataAccount.COL_ID, wx.LIST_FORMAT_LEFT, -1),
                (CDataAccount.COL_BRIEFNAME, wx.LIST_FORMAT_LEFT, -1),
                (CDataAccount.COL_FULLNAME, wx.LIST_FORMAT_LEFT, 200),
                (CDataAccount.COL_MEMO, wx.LIST_FORMAT_LEFT, -1)]
        
        self.list = MyListCtrlDecorator(self.lcAccount, cols, CDataAccount)
        

    def OnBtnAddButton(self, event):
        dlg = DlgAccountEditor.create(self)
        dlg.ShowModal()
        self.list.refresh()

    def getSelection(self):
        item = self.list.getSeletion()
        if (item == -1):
            return None
        
        val = int(self.list.getItem(item, 0))
        return CDataAccount.getFirstInstance({CDataAccount.COL_ID:val})
        
    def OnBtnModifyButton(self, event):
        obj = self.getSelection()
        if (obj is None):
            dlg = wx.MessageDialog(self, u'请选择列表项目', u'提示',
                                    wx.OK|wx.ICON_ERROR)
            dlg.ShowModal()
            return
        
        dlg = DlgAccountEditor.create(self)
        dlg.setData(obj)
        dlg.ShowModal()
        self.list.refresh()

    def OnBtnDelButton(self, event):
        obj = self.getSelection()
        if (obj is None):
            dlg = wx.MessageDialog(self, u'请选择列表项目', u'提示',
                                    wx.OK|wx.ICON_ERROR)
            dlg.ShowModal()
            return
        
        obj.delete()
        self.list.refresh()

